package com.edusys.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao<T> {
	//4
		final public static String DRIVER="com.mysql.jdbc.Driver";
		final public static String URL="jdbc:mysql://127.0.0.1:3306/eduSys??useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&zeroDateTimeBehavior=convertToNull";
		final public static String UID="root";
		final public static String PWD="123456";
		
		//3
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		//2
		private void open_db() {
			try {
				conn=DriverManager.getConnection(URL,UID,PWD);			
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		protected void close_db() {
				try {
					if (rs!=null) {
					rs.close();
					}
					if (pst!=null) {
						pst.close();
					}
					if (conn!=null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		
		/**
		 * 查询方法
		 * @param sql
		 * @return
		 * @throws SQLException
		 */
		protected ResultSet getData(String sql) throws SQLException {
			open_db();
			//打开数据库
			pst=conn.prepareStatement(sql);
			//查询
			rs=pst.executeQuery();		
			return rs;
		}
		
		/**
		 * 更新方法
		 * @param sql
		 * @return
		 */
		protected int modify(String sql) {
			int result=0;
			try {
				open_db();
				pst=conn.prepareStatement(sql);
				result=pst.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				close_db();
			}
			return result;
		}
		//1
		static {
			try {
				Class.forName(DRIVER);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
}
